package com.ymatou.iapi.couponactivity.testcase;

import java.util.ArrayList;
import java.util.List;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.couponactivity.parameter.NewUserMobileBoundBean;
import com.ymatou.iapi.couponactivity.parameter.SendNewUserMobileBoundCouponBean;
import com.ymatou.iapi.couponactivity.service.NewUserMobileBoundCall;
import com.ymatou.iapi.couponactivity.service.SendNewUserMobileBoundCouponCall;
import com.ymt.core.tool.Logger;
import com.ymt.util.DataManager;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.business.service.CouponActivityService;
import com.ymttest.database.model.YmtCoupon;
import com.ymttest.database.model.YmtCouponActivity;
import com.ymttest.database.model.YmtCouponActivityBatch;
import com.ymttest.database.model.YmtCouponActivityNewUserBound;
import com.ymttest.database.model.YmtCouponBatch;
import com.ymttest.database.model.YmtCouponPrivateUserBound;
import com.ymttest.database.model.YmtCouponSetting;
import com.ymttest.database.sqlwapper.YmtCouponActivityWapper;
import com.ymttest.database.sqlwapper.YmtCouponBatchWapper;
import com.ymttest.database.sqlwapper.YmtCouponPrivateUserBoundWapper;
import com.ymttest.database.sqlwapper.YmtCouponSettingWapper;
import com.ymttest.database.sqlwapper.YmtCouponWapper;
import com.ymttest.utils.TestDataManager;

/**
 * 发送手机绑定新人券用例 FreeMaker Template自动生成代码
 */
@TestSuite
public class Ts_SendNewUserMobileBoundCoupon {
	private static SendNewUserMobileBoundCouponBean sendnewusermobileboundcouponBean;
	private static SendNewUserMobileBoundCouponCall sendnewusermobileboundcouponCall;

	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("发送手机绑定新人券用例");
	}

	@Before
	public void caseUp() {
		sendnewusermobileboundcouponBean = new SendNewUserMobileBoundCouponBean();
		sendnewusermobileboundcouponCall = new SendNewUserMobileBoundCouponCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(sendnewusermobileboundcouponCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}

	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_SendNewUserMobileBoundCoupon_001() {
		Logger.start(true, "发送手机绑定新人券成功SELECT *  FROM [YmtRelease].[dbo].[Ymt_CouponActivityBatch]   where [ActivityCode]=16 and [SendItemNo]=1/2 中CouponBatchCodes需要改成有效批次");
		try {
			CouponActivityService couponActivityService = new CouponActivityService();
			YmtCouponActivityWapper ymtCouponActivityWapper = new YmtCouponActivityWapper();
			
			NewUserMobileBoundBean newUserMobileBoundBean = couponActivityService.setNewUserMobileBoundBean();			
			NewUserMobileBoundCall newUserMobileBoundCall = couponActivityService.NewUserMobileBound(newUserMobileBoundBean);
			
			YmtCouponActivity activity = ymtCouponActivityWapper.selectCouponActivityByActivityChannel("common").get(0);
			
			List<YmtCouponActivityBatch> batchs = ymtCouponActivityWapper.selectYmtCouponActivityBatchByActivityCode(activity.getActivityCode());
			
			List<Integer> receiveCountlist = new ArrayList<>();
			List<Integer> settingIdlist = new ArrayList<>();
			
			String batchCode="";
			
			for (YmtCouponActivityBatch activityBatch : batchs) {
				String batchCodeStr = activityBatch.getCouponBatchCodes();
				String batchCodes[] = batchCodeStr.split(",");
				batchCode = batchCodes[batchCodes.length-1];
				
				YmtCouponBatch batch = new YmtCouponBatchWapper().selectBysBatchCode(batchCode).get(0);
				YmtCouponSetting setting = new YmtCouponSettingWapper().selectByiCouponSettingId(batch.getiCouponSettingId()).get(0);
				settingIdlist.add(setting.getiCouponSettingId());
				receiveCountlist.add(setting.getiReceiveCount());
			}
			
			int userId = 30000000+Integer.parseInt(TestDataManager.getRandomNum(7));
			
			sendnewusermobileboundcouponBean.setMobile(newUserMobileBoundBean.getMobilePhone());
			sendnewusermobileboundcouponBean.setUserId(userId);
			sendnewusermobileboundcouponBean.setChangeMobileEventSource("Register");

			sendnewusermobileboundcouponCall.setData(sendnewusermobileboundcouponBean);
			sendnewusermobileboundcouponCall.callService();
			
			Logger.verifyEquals("\"ok\"", sendnewusermobileboundcouponCall.getReturnData(), "验证返回code");

			List<YmtCouponActivityNewUserBound> bounds = new YmtCouponActivityWapper().selectCouponActivityNewUserBoundByMobilePhoneAndActivityCode(newUserMobileBoundBean.getMobilePhone(), activity.getActivityCode());
			
			Logger.verifyEquals(1, bounds.size(), "验证数据库表Ymt_CouponActivityNewUserBound存在一条记录");
			
			if(bounds.size()==1)
			{
				YmtCouponActivityNewUserBound bound = bounds.get(0);
				//已发放
				Logger.verifyEquals(2, bound.getCouponSendStatus().intValue(), "验证数据库表Ymt_CouponActivityNewUserBound中字段CouponSendStatus为1");

				for(int i=0;i<settingIdlist.size();i++)
				{
					YmtCouponSetting setting = new YmtCouponSettingWapper().selectByiCouponSettingId(settingIdlist.get(i)).get(0);
					Logger.verifyEquals(1, setting.getiReceiveCount()-receiveCountlist.get(i), "验证返回Ymt_CouponSetting表iSettingId="+settingIdlist.get(i)+"的记录中iReceiveCount多了1");
				}
				
				for (YmtCouponActivityBatch activityBatch : batchs) {
					YmtCouponBatch batch = new YmtCouponBatchWapper().selectBysBatchCode(batchCode).get(0);
					
					List<YmtCouponPrivateUserBound> ymtCouponPrivateUserBounds = new YmtCouponPrivateUserBoundWapper().selectByBatchIdAndUserId(batch.getiBatchId(), userId);
					Logger.verifyEquals(1, ymtCouponPrivateUserBounds.size(), "验证返回YmtCouponPrivateUserBound生成一条记录");

					List<YmtCoupon> coupons = new YmtCouponWapper().selectBysCouponCode(ymtCouponPrivateUserBounds.get(0).getsCouponCode());
					Logger.verifyEquals(1, coupons.size(), "验证返回YmtCoupon生成一条记录");
					
				}
				
			}
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P3.class })
	@TestCase
	public void Tc_SendNewUserMobileBoundCoupon_002() {
		Logger.start(false, "验证ChangeMobileEventSource空时不发送优惠券");
		try {
			
			YmtCouponActivityWapper ymtCouponActivityWapper = new  YmtCouponActivityWapper();
			
			YmtCouponActivityNewUserBound bound = new YmtCouponActivityWapper().selectTop1YmtCouponActivityNewUserBoundByCouponSendStatus(1).get(0);
			
			YmtCouponActivity activity = ymtCouponActivityWapper.selectCouponActivityByActivityCode(bound.getActivityCode()).get(0);
			List<YmtCouponActivityBatch> batchs = ymtCouponActivityWapper.selectYmtCouponActivityBatchByActivityCode(activity.getActivityCode());
			
			List<Integer> receiveCountlist = new ArrayList<>();
			List<Integer> settingIdlist = new ArrayList<>();
			
			String batchCode="";
			
			for (YmtCouponActivityBatch activityBatch : batchs) {
				String batchCodeStr = activityBatch.getCouponBatchCodes();
				String batchCodes[] = batchCodeStr.split(",");
				batchCode = batchCodes[batchCodes.length-1];
	
				List<YmtCouponBatch> batches = new YmtCouponBatchWapper().selectBysBatchCode(batchCode);
				
				if(batches.size()==0)
				{
					settingIdlist.add(0);
					receiveCountlist.add(0);
				}
				else {
					YmtCouponBatch batch=batches.get(0);
					YmtCouponSetting setting = new YmtCouponSettingWapper().selectByiCouponSettingId(batch.getiCouponSettingId()).get(0);
					settingIdlist.add(setting.getiCouponSettingId());
					receiveCountlist.add(setting.getiReceiveCount());
				}
				
			}
			
			int userId = 30000000+Integer.parseInt(TestDataManager.getRandomNum(7));
			
			sendnewusermobileboundcouponBean.setMobile(bound.getMobilePhone());
			sendnewusermobileboundcouponBean.setUserId(userId);
			//sendnewusermobileboundcouponBean.setChangeMobileEventSource("Register");

			sendnewusermobileboundcouponCall.setData(sendnewusermobileboundcouponBean);
			sendnewusermobileboundcouponCall.callService();

			Logger.verifyEquals(1, bound.getCouponSendStatus().intValue(), "验证数据库表Ymt_CouponActivityNewUserBound中字段CouponSendStatus为1");

			for(int i=0;i<settingIdlist.size();i++)
			{
				if(settingIdlist.get(i)!=0)
				{
					YmtCouponSetting setting = new YmtCouponSettingWapper().selectByiCouponSettingId(settingIdlist.get(i)).get(0);
					Logger.verifyEquals(0, setting.getiReceiveCount()-receiveCountlist.get(i), "验证返回Ymt_CouponSetting表iSettingId="+settingIdlist.get(i)+"的记录中iReceiveCount未变");
				}
			}
			
			for (YmtCouponActivityBatch activityBatch : batchs) {
				List<YmtCouponBatch> batches = new YmtCouponBatchWapper().selectBysBatchCode(batchCode);
				
				if(batches.size()>0)
				{
					YmtCouponBatch batch = batches.get(0);
					List<YmtCouponPrivateUserBound> ymtCouponPrivateUserBounds = new YmtCouponPrivateUserBoundWapper().selectByBatchIdAndUserId(batch.getiBatchId(), userId);
					Logger.verifyEquals(0, ymtCouponPrivateUserBounds.size(), "验证返回YmtCouponPrivateUserBound未生成记录");
				}
				
			}
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P3.class })
	@TestCase
	public void Tc_SendNewUserMobileBoundCoupon_003() {
		Logger.start(false, "验证ChangeMobileEventSource非Register时不发送优惠券");
		try {
			
			YmtCouponActivityWapper ymtCouponActivityWapper = new  YmtCouponActivityWapper();
			
			YmtCouponActivityNewUserBound bound = new YmtCouponActivityWapper().selectTop1YmtCouponActivityNewUserBoundByCouponSendStatus(1).get(0);
			
			YmtCouponActivity activity = ymtCouponActivityWapper.selectCouponActivityByActivityCode(bound.getActivityCode()).get(0);
			List<YmtCouponActivityBatch> batchs = ymtCouponActivityWapper.selectYmtCouponActivityBatchByActivityCode(activity.getActivityCode());
			
			List<Integer> receiveCountlist = new ArrayList<>();
			List<Integer> settingIdlist = new ArrayList<>();
			
			String batchCode="";
			
			for (YmtCouponActivityBatch activityBatch : batchs) {
				String batchCodeStr = activityBatch.getCouponBatchCodes();
				String batchCodes[] = batchCodeStr.split(",");
				batchCode = batchCodes[batchCodes.length-1];
	
				List<YmtCouponBatch> batches = new YmtCouponBatchWapper().selectBysBatchCode(batchCode);
				
				if(batches.size()==0)
				{
					settingIdlist.add(0);
					receiveCountlist.add(0);
				}
				else {
					YmtCouponBatch batch=batches.get(0);
					YmtCouponSetting setting = new YmtCouponSettingWapper().selectByiCouponSettingId(batch.getiCouponSettingId()).get(0);
					settingIdlist.add(setting.getiCouponSettingId());
					receiveCountlist.add(setting.getiReceiveCount());
				}
				
			}
			
			int userId = 30000000+Integer.parseInt(TestDataManager.getRandomNum(7));
			
			sendnewusermobileboundcouponBean.setMobile(bound.getMobilePhone());
			sendnewusermobileboundcouponBean.setUserId(userId);
			sendnewusermobileboundcouponBean.setChangeMobileEventSource("Register1");

			sendnewusermobileboundcouponCall.setData(sendnewusermobileboundcouponBean);
			sendnewusermobileboundcouponCall.callService();

			Logger.verifyEquals(1, bound.getCouponSendStatus().intValue(), "验证数据库表Ymt_CouponActivityNewUserBound中字段CouponSendStatus为1");

			for(int i=0;i<settingIdlist.size();i++)
			{
				if(settingIdlist.get(i)!=0)
				{
					YmtCouponSetting setting = new YmtCouponSettingWapper().selectByiCouponSettingId(settingIdlist.get(i)).get(0);
					Logger.verifyEquals(0, setting.getiReceiveCount()-receiveCountlist.get(i), "验证返回Ymt_CouponSetting表iSettingId="+settingIdlist.get(i)+"的记录中iReceiveCount未变");
				}
			}
			
			for (YmtCouponActivityBatch activityBatch : batchs) {
				List<YmtCouponBatch> batches = new YmtCouponBatchWapper().selectBysBatchCode(batchCode);
				
				if(batches.size()>0)
				{
					YmtCouponBatch batch = batches.get(0);
					List<YmtCouponPrivateUserBound> ymtCouponPrivateUserBounds = new YmtCouponPrivateUserBoundWapper().selectByBatchIdAndUserId(batch.getiBatchId(), userId);
					Logger.verifyEquals(0, ymtCouponPrivateUserBounds.size(), "验证返回YmtCouponPrivateUserBound未生成记录");
				}
				
			}
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P3.class })
	@TestCase
	public void Tc_SendNewUserMobileBoundCoupon_004() {
		Logger.start(false, "手机号码不能为空");
		try {
			
			YmtCouponActivityNewUserBound bound = new YmtCouponActivityWapper().selectTop1YmtCouponActivityNewUserBoundByCouponSendStatus(1).get(0);
			int userId = 30000000+Integer.parseInt(TestDataManager.getRandomNum(7));
			
			//sendnewusermobileboundcouponBean.setMobile(bound.getMobilePhone());
			sendnewusermobileboundcouponBean.setUserId(userId);
			sendnewusermobileboundcouponBean.setChangeMobileEventSource("Register");

			sendnewusermobileboundcouponCall.setData(sendnewusermobileboundcouponBean);
			sendnewusermobileboundcouponCall.callService();

			Logger.verifyEquals("\"手机号码不能为空\"", sendnewusermobileboundcouponCall.getReturnData(), "验证数据库表Ymt_CouponActivityNewUserBound中字段CouponSendStatus为1");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({ P3.class })
	@TestCase
	public void Tc_SendNewUserMobileBoundCoupon_005() {
		Logger.start(false, "手机号未绑定过");
		try {
			
			int userId = 30000000+Integer.parseInt(TestDataManager.getRandomNum(7));
			
			sendnewusermobileboundcouponBean.setMobile(TestDataManager.getRandomNum(11));
			sendnewusermobileboundcouponBean.setUserId(userId);
			sendnewusermobileboundcouponBean.setChangeMobileEventSource("Register");

			sendnewusermobileboundcouponCall.setData(sendnewusermobileboundcouponBean);
			sendnewusermobileboundcouponCall.callService();

			Logger.verifyEquals("\"ok\"", sendnewusermobileboundcouponCall.getReturnData(), "验证数据库表Ymt_CouponActivityNewUserBound中字段CouponSendStatus为1");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({ P3.class })
	@TestCase
	public void Tc_SendNewUserMobileBoundCoupon_006() {
		Logger.start(false, "已发放过");
		try {

			YmtCouponActivityWapper ymtCouponActivityWapper = new  YmtCouponActivityWapper();
			
			YmtCouponActivityNewUserBound bound = new YmtCouponActivityWapper().selectTop1YmtCouponActivityNewUserBoundByCouponSendStatus(2).get(0);
			
			YmtCouponActivity activity = ymtCouponActivityWapper.selectCouponActivityByActivityCode(bound.getActivityCode()).get(0);
			List<YmtCouponActivityBatch> batchs = ymtCouponActivityWapper.selectYmtCouponActivityBatchByActivityCode(activity.getActivityCode());
			
			List<Integer> receiveCountlist = new ArrayList<>();
			List<Integer> settingIdlist = new ArrayList<>();
			
			String batchCode="";
			
			for (YmtCouponActivityBatch activityBatch : batchs) {
				String batchCodeStr = activityBatch.getCouponBatchCodes();
				String batchCodes[] = batchCodeStr.split(",");
				batchCode = batchCodes[batchCodes.length-1];
				
				YmtCouponBatch batch = new YmtCouponBatchWapper().selectBysBatchCode(batchCode).get(0);
				YmtCouponSetting setting = new YmtCouponSettingWapper().selectByiCouponSettingId(batch.getiCouponSettingId()).get(0);
				settingIdlist.add(setting.getiCouponSettingId());
				receiveCountlist.add(setting.getiReceiveCount());
			}
			
			int userId = 30000000+Integer.parseInt(TestDataManager.getRandomNum(7));
			
			sendnewusermobileboundcouponBean.setMobile(bound.getMobilePhone());
			sendnewusermobileboundcouponBean.setUserId(userId);
			sendnewusermobileboundcouponBean.setChangeMobileEventSource("Register");

			sendnewusermobileboundcouponCall.setData(sendnewusermobileboundcouponBean);
			sendnewusermobileboundcouponCall.callService();

			for(int i=0;i<settingIdlist.size();i++)
			{
				YmtCouponSetting setting = new YmtCouponSettingWapper().selectByiCouponSettingId(settingIdlist.get(i)).get(0);
				Logger.verifyEquals(0, setting.getiReceiveCount()-receiveCountlist.get(i), "验证返回Ymt_CouponSetting表iSettingId="+settingIdlist.get(i)+"的记录中iReceiveCount未变");
			}
			
			for (YmtCouponActivityBatch activityBatch : batchs) {
				YmtCouponBatch batch = new YmtCouponBatchWapper().selectBysBatchCode(batchCode).get(0);
				
				List<YmtCouponPrivateUserBound> ymtCouponPrivateUserBounds = new YmtCouponPrivateUserBoundWapper().selectByBatchIdAndUserId(batch.getiBatchId(), userId);
				Logger.verifyEquals(0, ymtCouponPrivateUserBounds.size(), "验证返回YmtCouponPrivateUserBound未生成记录");
				
			}
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({ P1.class })
	@TestCase
	@Ignore
	public void Tc_SendNewUserMobileBoundCoupon_007() {
		Logger.start(true, "等级为1的用户发放优惠券失败-未生成优惠券记录");
		try {
		
			YmtCouponActivityWapper ymtCouponActivityWapper = new  YmtCouponActivityWapper();
			
			YmtCouponActivityNewUserBound bound = new YmtCouponActivityWapper().selectTop1YmtCouponActivityNewUserBoundByCouponSendStatus(2).get(0);
			
			YmtCouponActivity activity = ymtCouponActivityWapper.selectCouponActivityByActivityCode(bound.getActivityCode()).get(0);
			List<YmtCouponActivityBatch> batchs = ymtCouponActivityWapper.selectYmtCouponActivityBatchByActivityCode(activity.getActivityCode());
			
			List<Integer> receiveCountlist = new ArrayList<>();
			List<Integer> settingIdlist = new ArrayList<>();
			
			String batchCode="";
			
			for (YmtCouponActivityBatch activityBatch : batchs) {
				String batchCodeStr = activityBatch.getCouponBatchCodes();
				String batchCodes[] = batchCodeStr.split(",");
				batchCode = batchCodes[batchCodes.length-1];
				
				YmtCouponBatch batch = new YmtCouponBatchWapper().selectBysBatchCode(batchCode).get(0);
				YmtCouponSetting setting = new YmtCouponSettingWapper().selectByiCouponSettingId(batch.getiCouponSettingId()).get(0);
				settingIdlist.add(setting.getiCouponSettingId());
				receiveCountlist.add(setting.getiReceiveCount());
			}
			
			int userId = 30000000+Integer.parseInt(TestDataManager.getRandomNum(7));
			
			sendnewusermobileboundcouponBean.setMobile("18516029204");
			sendnewusermobileboundcouponBean.setUserId(userId);
			sendnewusermobileboundcouponBean.setChangeMobileEventSource("Register");

			sendnewusermobileboundcouponCall.setData(sendnewusermobileboundcouponBean);
			sendnewusermobileboundcouponCall.callService();

			Logger.verifyEquals("\"ok\"", sendnewusermobileboundcouponCall.getReturnData(), "验证返回");

			YmtCouponActivityNewUserBound bound2 = new YmtCouponActivityWapper().selectCouponActivityNewUserBoundByUserIdAndActivityCode(userId+"", bound.getActivityCode()).get(0);
			Logger.verifyEquals(3, bound2.getCouponSendStatus().intValue(), "验证数据库中Ymt_CouponActivityNewUserBound字段CouponSendStatus");
			Logger.verifyEquals("用户存在风险", bound2.getFailErrorDetail(), "验证数据库中Ymt_CouponActivityNewUserBound字段FailErrorDetail");
			Logger.verifyEquals("12001", bound2.getFailErrorCode(), "验证数据库中Ymt_CouponActivityNewUserBound字段FailErrorCode");
			
			for(int i=0;i<settingIdlist.size();i++)
			{
				YmtCouponSetting setting = new YmtCouponSettingWapper().selectByiCouponSettingId(settingIdlist.get(i)).get(0);
				Logger.verifyEquals(0, setting.getiReceiveCount()-receiveCountlist.get(i), "验证返回Ymt_CouponSetting表iSettingId="+settingIdlist.get(i)+"的记录中iReceiveCount未变");
			}
			
			for (YmtCouponActivityBatch activityBatch : batchs) {
				YmtCouponBatch batch = new YmtCouponBatchWapper().selectBysBatchCode(batchCode).get(0);
				
				List<YmtCouponPrivateUserBound> ymtCouponPrivateUserBounds = new YmtCouponPrivateUserBoundWapper().selectByBatchIdAndUserId(batch.getiBatchId(), userId);
				Logger.verifyEquals(0, ymtCouponPrivateUserBounds.size(), "验证返回YmtCouponPrivateUserBound未生成记录");
				
			}		
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

}